package com.deryun.tyl.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.ContentMessage;
import com.deryun.tyl.bean.IndexImage;
import com.deryun.tyl.bean.User;
import com.deryun.tyl.service.impl.UserServiceImpl;

@Controller
@RequestMapping("user")
public class UserController {
	
	@Resource(name="userService")
	private UserServiceImpl userService;
	
	
	
	@RequestMapping(value="doLogin",method=RequestMethod.POST)
	public String doLogin(User user,HttpServletRequest request){
		if (userService.doLogin(user.getUsername(), user.getPassword())!=null) {
			user = userService.getUserByUsername(user.getUsername());
			request.getSession().setAttribute("user",user);
			return "back_admin_manger/homepage";
		}else {
			return "redirect:/civil/homepage";
		}
	}
	
	@RequestMapping("/addUser")
	public String register(User user,HttpServletRequest request){
		userService.addUser(user);
		request.getSession().setAttribute("user", user);
		return "redirect:/civil/homepage";
	}
	

	//添加用户
	@RequestMapping("/addUserStep1")
	public String addUserStep1(User user,HttpServletRequest request){
		request.getSession().setAttribute("user",user);
		return "redirect:/civil/register2";
	}
	
	//校验名字
	@RequestMapping("/validate")
	@ResponseBody
	public int validate(User user){
		if (userService.validateNames(user)==null) {
			return 0;
		}
		return 1;
	}
	
	//添加机构
	@RequestMapping("/addUserStep2")
	public String addUserStep2(User user, @RequestParam("institutionPicPath1") MultipartFile oneKeyPic1, HttpServletRequest request) throws IllegalStateException, IOException{
		String path = request.getSession().getServletContext().getRealPath("upload");  
		if (!oneKeyPic1.isEmpty()) {
			String oneKeyPicFileName = String.valueOf(System.currentTimeMillis()) + ".png";
			File oneKeyPicFile = new File(path, oneKeyPicFileName);  
			if(!oneKeyPicFile.exists())
			{  
				oneKeyPicFile.mkdirs();  
			}  
			oneKeyPic1.transferTo(oneKeyPicFile);  
			user.setInstitutionPicPath("/upload/"+oneKeyPicFileName);
		}
		User tuser = (User) request.getSession().getAttribute("user");
		user.setInstitutionName(tuser.getInstitutionName());
		user.setUsername(tuser.getUsername());
		user.setPassword(tuser.getPassword());
		user.setContactMan(tuser.getContactMan());
		user.setContactPhone(tuser.getContactPhone());
		user.setEmail(tuser.getEmail());
		user.setRole(tuser.getRole());
		user.setInstitutionAuth("1");
		request.getSession().setAttribute("user",user);
		return "redirect:/civil/register3";
	}
	//认证并提交
	@RequestMapping("/addUserStep3")
	public String addUserStep3(@RequestParam("authType") int authType,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (authType == 1) {
			user.setInstitutionOnekeyAuth("1");
		}else {
			user.setInstitutionBankAuth("1");
		}
		if (userService.addUser(user) > 0) {
			return "redirect:/civil/register4";
		}
		return "redirect:/civil/register5";
	}
	
	//认证页面
	@RequestMapping("toMyAuthStat")
	public String toMyProject(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		return "back_admin_manger/myAuthStat";
	}
	
	//认证机构
	@RequestMapping("authInstitution")
	public String authInstitution(User tuser,@RequestParam("institutionPicPath1") MultipartFile institutionPicPath1, HttpServletRequest request) throws IllegalStateException, IOException{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		String path = request.getSession().getServletContext().getRealPath("upload");
		tuser.setId(user.getId());
		if (!institutionPicPath1.isEmpty()) 
		{
			 // 自定义的文件名称
			String editImgOneName= String.valueOf(System.currentTimeMillis()) + ".png";
			File imgOneFile = new File(path, editImgOneName);  
			if(!imgOneFile.exists())
			{  
				imgOneFile.mkdirs();  
			}  
			institutionPicPath1.transferTo(imgOneFile);
			tuser.setInstitutionPicPath("/upload/"+editImgOneName);
		}
		int res = userService.authInstitution(tuser);
		user.setInstitutionAuth("1");
		request.getSession().setAttribute("user", user);
		return "back_admin_manger/myAuthStat";
	}
	
	//认证一键通
	@RequestMapping("authOneKey")
	public String authOneKey(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (userService.authOneKey(user.getId().toString())==1) {
			user.setInstitutionOnekeyAuth("1");
			request.getSession().setAttribute("user", user);
		}
		user.setInstitutionOnekeyAuth("1");
		request.getSession().setAttribute("user", user);
		return "back_admin_manger/myAuthStat";
	}
	
	//认证银行
	@RequestMapping("authBank")
	public String authBank(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (userService.authBank(user.getId().toString())==1) {
			user.setInstitutionBankAuth("1");
			request.getSession().setAttribute("user", user);
		}
		user.setInstitutionBankAuth("1");
		request.getSession().setAttribute("user", user);
		return "back_admin_manger/myAuthStat";
	}
	
	//获取账号详细信息
	@RequestMapping("getAccount")
	public String getAccount(@RequestParam("id") int id, HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		User tuser = userService.getUserById(id);
		request.setAttribute("message", tuser);
		return "back_admin_manger/account_detail";
	}
	
	//查询机构
	@RequestMapping("searchUser")
	public String searchUser(@RequestParam("role") String role,@RequestParam("institutionName") String institutionName,@RequestParam("type") String type, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		List<User> message = userService.searchUser(role, institutionName);
		request.setAttribute("message", message);
		if (type.equals("1")) {
			return "back_admin_manger/institution_base";
		}
		return "back_admin_manger/auth_base";
	}
	
	//查看用户详细
	@RequestMapping("getUser")
	public String getUser(@RequestParam("id") int id, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		User tuser = userService.getUserById(id);
		request.setAttribute("message", tuser);
		return "back_admin_manger/account_detail";
	}
	
	//认证账号详情
	@RequestMapping("adminAuth")
	public String adminAuth(User user, HttpServletRequest request){
		User tuser = (User) request.getSession().getAttribute("user");
		if (tuser == null) 
		{
			return "redirect:/civil/homepage";
		}
		userService.accessAuth(user);
		return "redirect:/ftoac/toAccountAuth";
	}
	
	//查询机构
	@RequestMapping("searchAuth")
	public String searchAuth(@RequestParam("auth") String auth,@RequestParam("institutionName") String institutionName,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		List<User> message = userService.searchAuth(auth, institutionName);
		request.setAttribute("message", message);
		return "back_admin_manger/auth_base";
	}
	
	@RequestMapping("/wqew")
	public String toMyContent(){
		return "";
	}
	
	//设置上首页
	@RequestMapping("toFrontSupplier")
	public String toFrontSupplier(@RequestParam("id") int id, HttpServletRequest request){
		User tuser = (User) request.getSession().getAttribute("user");
		if (tuser == null) 
		{
			return "redirect:/civil/homepage";
		}
		if (userService.getCountFront() >= 5) {
			request.setAttribute("about", "failed");
			return "forward:/ftoac/toSupplierToFront";
		}
		userService.toFrontSupplier(id);
		return "redirect:/ftoac/toSupplierToFront";
	}
	
	//取消上首页
	@RequestMapping("removeFrontSupplier")
	public String removeFrontSupplier(@RequestParam("id") int id, HttpServletRequest request){
		User tuser = (User) request.getSession().getAttribute("user");
		if (tuser == null) 
		{
			return "redirect:/civil/homepage";
		}
		userService.removeFrontSupplier(id);
		return "redirect:/ftoac/toSupplierToFront";
	}
	
	//设置上一键通
	@RequestMapping("toOneKeySupplier")
	public String toOneKeySupplier(@RequestParam("id") int id, HttpServletRequest request){
		User tuser = (User) request.getSession().getAttribute("user");
		if (tuser == null) 
		{
			return "redirect:/civil/homepage";
		}
		if (userService.getCountFront() >= 7) {
			request.setAttribute("about", "failed");
			return "redirect:/ftoac/toSupplierToFront";
		}
		userService.toOneKeySupplier(id);
		return "redirect:/ftoac/toSupplierToFront";
	}
	
	//取消上一键通
	@RequestMapping("removeOneKeySupplier")
	public String removeOneKeySupplier(@RequestParam("id") int id, HttpServletRequest request){
		User tuser = (User) request.getSession().getAttribute("user");
		if (tuser == null) 
		{
			return "redirect:/civil/homepage";
		}
		userService.removeOneKeySupplier(id);
		return "redirect:/ftoac/toSupplierToFront";
	}
	
}
